<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 收藏商品数据
const favoriteProducts = ref([
  {
    id: 1,
    name: '无线蓝牙耳机',
    price: 299,
    originalPrice: 399,
    discount: '7.5折',
    image: 'https://picsum.photos/seed/earphones/400/300',
    category: '数码',
    rating: 4.8,
    sales: 1258
  },
  {
    id: 3,
    name: '智能手表',
    price: 1299,
    originalPrice: 1599,
    discount: '8.1折',
    image: 'https://picsum.photos/seed/watch/400/300',
    category: '数码',
    rating: 4.7,
    sales: 890
  },
  {
    id: 5,
    name: '时尚连衣裙',
    price: 299,
    originalPrice: 459,
    discount: '6.5折',
    image: 'https://picsum.photos/seed/dress/400/300',
    category: '服饰',
    rating: 4.8,
    sales: 2345
  }
])

// 加载收藏数据
onMounted(() => {
  // 实际项目中，这里应该从localStorage或API获取收藏数据
  const savedFavorites = localStorage.getItem('favorites')
  if (savedFavorites) {
    try {
      favoriteProducts.value = JSON.parse(savedFavorites)
    } catch (e) {
      console.error('解析收藏数据失败', e)
    }
  }
})

// 保存收藏数据到localStorage
const saveFavorites = () => {
  localStorage.setItem('favorites', JSON.stringify(favoriteProducts.value))
}

// 取消收藏
const removeFromFavorites = (productId) => {
  const index = favoriteProducts.value.findIndex(item => item.id === productId)
  if (index !== -1) {
    favoriteProducts.value.splice(index, 1)
    saveFavorites()
  }
}

// 前往商品详情
const goToProductDetail = (productId) => {
  // 实际项目中，这里应该跳转到商品详情页
  router.push(`/mall?productId=${productId}`)
}

// 加入购物车
const addToCart = (product) => {
  // 实际项目中，这里应该调用添加购物车的API
  let cart = localStorage.getItem('cart')
  cart = cart ? JSON.parse(cart) : []
  
  // 检查商品是否已在购物车中
  const existingItem = cart.find(item => item.id === product.id)
  if (existingItem) {
    existingItem.quantity++
  } else {
    cart.push({ ...product, quantity: 1 })
  }
  
  localStorage.setItem('cart', JSON.stringify(cart))
  
  // 显示添加成功提示
  alert('已成功加入购物车')
}
</script>

<template>
  <div class="favorites-container">
    <!-- 页面标题 -->
    <header class="favorites-header">
      <h1>我的收藏</h1>
      <p class="favorites-subtitle">共 {{ favoriteProducts.length }} 件商品</p>
    </header>

    <!-- 收藏列表 -->
    <main class="favorites-main">
      <!-- 收藏为空时的提示 -->
      <div v-if="favoriteProducts.length === 0" class="empty-favorites">
        <div class="empty-icon">🎯</div>
        <h3>您还没有收藏任何商品</h3>
        <p>去商城逛逛，收藏喜欢的商品吧！</p>
        <button @click="router.push('/mall')" class="btn-go-shopping">
          前往商城
        </button>
      </div>

      <!-- 收藏商品列表 -->
      <div v-else class="favorites-list">
        <div v-for="product in favoriteProducts" :key="product.id" class="favorite-item">
          <!-- 商品图片 -->
          <div class="product-image" @click="goToProductDetail(product.id)">
            <img :src="product.image" :alt="product.name" />
          </div>

          <!-- 商品信息 -->
          <div class="product-info">
            <h3 class="product-name" @click="goToProductDetail(product.id)">{{ product.name }}</h3>
            <div class="product-meta">
              <span class="product-category">{{ product.category }}</span>
              <span class="product-rating">⭐ {{ product.rating }}</span>
              <span class="product-sales">{{ product.sales }}人已购</span>
            </div>
            <div class="product-price">
              <span class="current-price">¥{{ product.price }}</span>
              <span class="original-price">¥{{ product.originalPrice }}</span>
              <span class="discount">{{ product.discount }}</span>
            </div>
          </div>

          <!-- 操作按钮 -->
          <div class="product-actions">
            <button @click="addToCart(product)" class="btn-add-to-cart">
              加入购物车
            </button>
            <button @click="removeFromFavorites(product.id)" class="btn-remove-favorite">
              取消收藏
            </button>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<style scoped>
.favorites-container {
  min-height: 100vh;
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
  color: #333;
  background-color: #f8f9fa;
}

/* 页面标题 */
.favorites-header {
  background-color: white;
  padding: 2rem 0;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.favorites-header h1 {
  font-size: 2rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: #333;
}

.favorites-subtitle {
  color: #666;
  font-size: 1rem;
}

/* 主要内容区 */
.favorites-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

/* 收藏为空时的样式 */
.empty-favorites {
  text-align: center;
  padding: 6rem 2rem;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1.5rem;
}

.empty-favorites h3 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
  color: #333;
}

.empty-favorites p {
  color: #666;
  margin-bottom: 2rem;
}

.btn-go-shopping {
  background-color: #667eea;
  color: white;
  border: none;
  padding: 12px 24px;
  font-size: 1rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-go-shopping:hover {
  background-color: #5a67d8;
}

/* 收藏列表 */
.favorites-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.favorite-item {
  display: flex;
  align-items: center;
  background-color: white;
  border-radius: 8px;
  padding: 1rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: transform 0.2s, box-shadow 0.2s;
}

.favorite-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 商品图片 */
.product-image {
  flex-shrink: 0;
  width: 120px;
  height: 120px;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.product-image:hover img {
  transform: scale(1.05);
}

/* 商品信息 */
.product-info {
  flex: 1;
  margin: 0 1.5rem;
}

.product-name {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 0.5rem;
  color: #333;
  cursor: pointer;
  transition: color 0.3s;
}

.product-name:hover {
  color: #667eea;
}

.product-meta {
  display: flex;
  gap: 1rem;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
  color: #666;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.current-price {
  font-size: 1.3rem;
  font-weight: 600;
  color: #e53e3e;
}

.original-price {
  font-size: 0.9rem;
  color: #999;
  text-decoration: line-through;
}

.discount {
  font-size: 0.8rem;
  color: #e53e3e;
  background-color: #fff5f5;
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
}

/* 操作按钮 */
.product-actions {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.btn-add-to-cart {
  background-color: #667eea;
  color: white;
  border: none;
  padding: 8px 16px;
  font-size: 0.9rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-add-to-cart:hover {
  background-color: #5a67d8;
}

.btn-remove-favorite {
  background-color: #f7fafc;
  color: #4a5568;
  border: 1px solid #e2e8f0;
  padding: 8px 16px;
  font-size: 0.9rem;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-remove-favorite:hover {
  background-color: #edf2f7;
  border-color: #cbd5e0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .favorite-item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .product-image {
    width: 100%;
    height: 200px;
    margin-bottom: 1rem;
  }
  
  .product-info {
    margin: 0 0 1rem 0;
    width: 100%;
  }
  
  .product-actions {
    width: 100%;
    flex-direction: row;
  }
  
  .btn-add-to-cart,
  .btn-remove-favorite {
    flex: 1;
  }
}
</style>